package so.hands.xam.core; 

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

/** 
 * @Title: TestRun.java  
 * @Package so.hands.Xautomation.core 
 * @Description: TODO(测试引擎) 
 * @author 朱晓峰 
 * @date 2012-9-15 下午04:54:39 
 * @version V1.0 
 */
public class TestRun {
	
	//selenium测试集
	public Selenium selenium;
	//selenium服务地址
	private String host;
	//selenium服务端口
	private int port;
	//测试地址
	private String url;
	//测试浏览器
	/*Supported browsers include:
	  *firefox
	  *mock
	  *firefoxproxy
	  *pifirefox
	  *chrome
	  *iexploreproxy
	  *iexplore
	  *firefox3
	  *safariproxy
	  *googlechrome
	  *konqueror
	  *firefox2
	  *safari
	  *piiexplore
	  *firefoxchrome
	  *opera
	  *webdriver
	  *iehta
	  *custom*/
	private String browser;
	//操作符数组
	private String[] oper= {"type","click","doubleClick","select"};
	
	/**
	 * @title 运行于本地的selenium服务，不需指定端口和服务地址
	 * @param url 指定测试地址
	 * @param browser 指定测试浏览器
	 */
	public TestRun(String url,String browser){
		this.port = SeleniumServerController.getRcc().getPort();
		this.url = url;
		this.browser = browser;
		this.host = "127.0.0.1";
		this.OpenApp();
	}
	

	/**
	 * @title 运行于指定服务地址与端口
	 * @param host 指定目标服务地址
	 * @param port 指定服务端口
	 * @param url 指定测试地址
	 * @param browser 指定测试浏览器
	 */
	public TestRun(String host,int port,String url,String browser){
		this.port = port;
		this.url = url;
		this.browser = browser;
		this.host = host;
		this.OpenApp();
	}
	
	private int getOper(String oper){
		int i;
		for(i=0;i<12;i++){
			if(oper.equals(this.oper[i])){
				break;
			}
		}
		return i;
	}
	
	public void OpenApp(){
		DefaultSelenium selenium = new DefaultSelenium(host,port,browser,url);
		selenium.start();
	 	selenium.open(url);
	 	this.selenium = selenium;
	}
	
	
	/**
	 * @title 有输入值的操作
	 * @param locator selenium选择器
	 * @param oper 操作符
	 * @param value 输入值
	 */
	public void Run(String locator,String oper,String value){
		switch(this.getOper(oper)){
			case 0:
				this.selenium.type(locator, value);
				break;
			case 3:
				this.selenium.select(locator, value);
				break;
			default:
				System.out.println("操作符输入不正确！");
		}
	}
	

	/**
	 * @title 无输入值的操作
	 * @param locator selenium选择器
	 * @param oper 操作符
	 */
	public void Run(String locator,String oper){
		switch(this.getOper(oper)){
			case 1:
				this.selenium.click(locator);
				break;
			case 2:
				this.selenium.doubleClick(locator);
				break;
			default:
				System.out.println("操作符输入不正确！");
		}
	}
	
	//WebObject
//	public void Run(String id,String oper,String value){
//		
//	}
	
}
  
    